﻿
node_tips = {
    NODE_STATE = {
        no_active = 0,
        active = 1,
    };

    MAX_TIPS_CONTENT_LEN = 20;

    state_ = nil;

    tips_dlg_id_ = nil;
};

node_tips_event_listener = {
};

function node_tips:new(percent, tips_text, wrapper_id, game_id, index)
    local o = { percent_ = percent, 
                tips_text_ = tips_text,
                wrapper_id_ = wrapper_id,
                game_id_ = game_id, 
                index_ = index };

    setmetatable(o, self);
    self.__index = self;
    o:init();
    return o;
end

function node_tips:delete()
    if self.tips_dlg_id_ ~= nil then
        ui_utility.setproperty_by_id(self.tips_dlg_id_, "Visible", "false");
        layout_mgr.delay_remove_ctrl(self.tips_dlg_id_);
        self.tips_dlg_id_ = nil;
    end;
    
    if self.wrapper_id_ ~= nil then
        ui_utility.setproperty_by_id(ctrl_id, "Visible", "false");
        layout_mgr.delay_remove_ctrl(self.wrapper_id_);
        self.wrapper_id_ = nil;
    end;
end;

function node_tips:init()
    local main_wnd_id = ui_utility.get_ctrl_id_by_cid(CID_MAINFRAME_Dialog_MainFrame);
    if self.tips_dlg_id_ == nil then
        self.tips_dlg_id_ = layout_mgr.restore_sub_layout_multi(
                                main_wnd_id, "frame_bottom\\NodeTipsDlg.tpf");
    end;
end

-- state: 0: 未激活，1：激活
function node_tips:refresh_state(state)
    if self.state_ == state then
        return;
    end

    self.state_ = state;
    if state == self.NODE_STATE.no_active then
        local bg = "ONEIMAGE:frame_bottom\\point_off.png;Center;null;"
        ui_utility.setproperty_by_path(self.wrapper_id_, "NodeBtn", "Background", bg);
        --ui_utility.setproperty_by_path(self.wrapper_id_, "NodeBtn", "BackgroundHover", bg);
        --ui_utility.setproperty_by_path(self.wrapper_id_, "NodeBtn", "BackgroundPressed", bg);
    elseif state == self.NODE_STATE.active then
        local bg = "ONEIMAGE:frame_bottom\\point_on.png;Center;null;"
        ui_utility.setproperty_by_path(self.wrapper_id_, "NodeBtn", "Background", bg);
        --ui_utility.setproperty_by_path(self.wrapper_id_, "NodeBtn", "BackgroundHover", bg);
        --ui_utility.setproperty_by_path(self.wrapper_id_, "NodeBtn", "BackgroundPressed", bg); 
    end
end

function node_tips:show(show)
    if self.wrapper_id_ ~= nil then
        local visible = "true";
        if show == false then
            visible = "false";
        end;
        ui_utility.setproperty_by_id(self.wrapper_id_, "Visible", visible);
    end;
end

function node_tips:get_index()
    return self.index_;
end

function node_tips:set_percent(percent)
    self.percent_ = tonumber(percent);
end;

function node_tips:set_tips(tips)
    self.tips_text_ = tips;
end;

function node_tips:get_wrapper_id()
    return self.wrapper_id_;
end

function node_tips:show_tips(show)
    if self.tips_dlg_id_ == nil then
        return;
    end;
    local visible = "true";
    if show == false then
        visible = "false";
    end;

    -- 文字为空时不显示
    if self.tips_text_ == nil or self.tips_text_ == "" then
        visible = "false";
    end;

    ui_utility.setproperty_by_id(self.tips_dlg_id_, "Visible", visible);
end

function node_tips:refresh_node_pos(progress_bar_width)
    if progress_bar_width == nil or progress_bar_width == 0 then
        return;
    end;
    if self.wrapper_id_ == nil then
        return;
    end;
    local wrapper_ptr = ui_utility.get_ctrl_ptr_by_id(self.wrapper_id_);
    if wrapper_ptr == nil then
        return;
    end;

    local offset_x = math.floor((progress_bar_width * self.percent_ / 100.0)) - 12;
    local wrapper_rc = wrapper_ptr:GetRectEx(FALSE,TRUE);
    if wrapper_rc == nil then
        return;
    end;

    if offset_x ~= nil and offset_x >= 0 and offset_x <= progress_bar_width then
        local width = wrapper_rc.right - wrapper_rc.left;
        local height = wrapper_rc.bottom - wrapper_rc.top;
        wrapper_rc.left = offset_x;
        wrapper_rc.right = wrapper_rc.left + width;
        wrapper_rc.top = 0;
        wrapper_rc.bottom = wrapper_rc.top + height;
        wrapper_ptr:MoveEx(wrapper_rc,TRUE);
        
        self:refresh_tips_positon();
    end
end;

function node_tips:refresh_tips_positon()
    if self.wrapper_id_ == nil then
        return;
    end;
    local wrapper_ptr = ui_utility.get_ctrl_ptr_by_id(self.wrapper_id_);
    if wrapper_ptr == nil then
        return;
    end;
    local wrapper_rc = wrapper_ptr:GetRectEx(TRUE,TRUE);
    if wrapper_rc == nil then
        return;
    end;
    
    if self.tips_dlg_id_ == nil then
        return;
    end;
    local tips_dlg_ptr = ui_utility.get_ctrl_ptr_by_id(self.tips_dlg_id_);
    if tips_dlg_ptr == nil then
        return;
    end;
    local tips_dlg_rc = tips_dlg_ptr:GetRectEx(TRUE,TRUE);
    if tips_dlg_rc == nil then
        return;
    end;
    
    -- 长度截断
    local show_tips_str = U8toCUIStr(self.tips_text_);
    local need_cut = ui_utility.is_over_char_count(self.tips_text_, self.MAX_TIPS_CONTENT_LEN);
    if need_cut then
        show_tips_str = U8toCUIStrn(self.tips_text_, self.MAX_TIPS_CONTENT_LEN, 1);
    end
    local tips_txt_ptr = ui_utility.get_ctrl_ptr_by_path(self.tips_dlg_id_, "text");
    if tips_txt_ptr ~= nil then
        tips_txt_ptr:SetText(show_tips_str);
    end;

    -- 计算控件长度
    local tips_txt_u8 = CUIStrtoU8(show_tips_str);
    local tips_dlg_width = ui_utility.cal_str_width(tips_txt_u8);
    local width = tips_dlg_width + 20;
    local height = 35;
    ui_utility.setproperty_by_id(self.tips_dlg_id_, "Width", width);
    --ui_utility.setproperty_by_path(self.tips_dlg_id_, "text", "Caption", self.tips_text_);

    --获取主窗口的位置
    local main_wnd_ptr = ui_utility.get_ctrl_ptr_by_cid(CID_MAINFRAME_Dialog_MainFrame);
    if main_wnd_ptr == nil then
        return;
    end;
    local main_wnd_rc = main_wnd_ptr:GetRectEx(TRUE,TRUE);
    if main_wnd_rc == nil then
        return;
    end;

    if tips_dlg_rc.left == wrapper_rc.left - main_wnd_rc.left and 
       tips_dlg_rc.top == wrapper_rc.top - main_wnd_rc.top - height then
        return;
    end;

    tips_dlg_rc.left = wrapper_rc.left - main_wnd_rc.left;
    tips_dlg_rc.right = tips_dlg_rc.left + width;
    tips_dlg_rc.top = wrapper_rc.top - main_wnd_rc.top - height;
    tips_dlg_rc.bottom = tips_dlg_rc.top + height;
    tips_dlg_ptr:MoveEx(tips_dlg_rc,TRUE);
end;

----------------------------------------------------------------------------------------------------

function node_tips_event_listener.on_mouse_hover(uilib, control_id, x, y)
    -- control_id is UIMessageSpy control id
    local item_ctrl_id = ui_utility.get_ctrl_parent_id(control_id);
    local item = progress_node_tips_area.get_progress_node_item(item_ctrl_id);

    if nil == item then
        base_utility.log(string.format("[node_tips_event_listener.on_mouse_hover]item is NIL," ..
        " ctrl_id:%d", control_id), true);
        return;
    end
    item:refresh_tips_positon();
    item:show_tips(true);
end

function node_tips_event_listener.on_mouse_leave(uilib, control_id, x, y)
    -- control_id is UIMessageSpy control id
    local item_ctrl_id = ui_utility.get_ctrl_parent_id(control_id);
    local item = progress_node_tips_area.get_progress_node_item(item_ctrl_id);

    if nil == item then
        base_utility.log(string.format("[node_tips_event_listener.on_mouse_leave]item is NIL," ..
        " ctrl_id:%d", control_id), true);
        return;
    end

    item:show_tips(false);
end

function node_tips_event_listener.on_tips_dlg_click(uilib, control_id)
    if control_id == 0 or control_id == nil then
        return;
    end;
    ui_utility.setproperty_by_id(control_id, "Visible", "false");
    if wegame_mainframe_view ~= nil and wegame_mainframe_view.dialog_ptr ~= nil then
        wegame_mainframe_view.dialog_ptr:SetFocus();
    end;
end